package com.crm.CLdriving.service.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.dto.IdReqDto;
import com.crm.CLdriving.dto.req.SubInputReqDto;
import com.crm.CLdriving.dto.req.SubInsertReqDto;
import com.crm.CLdriving.dto.req.SubOneUpdateByIdReqDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.ExaminMapper;
import com.crm.CLdriving.mapper.SubTowMapper;
import com.crm.CLdriving.po.ExaminPO;
import com.crm.CLdriving.po.SubjectOnePO;
import com.crm.CLdriving.po.SubjectTowPO;
import com.crm.CLdriving.service.SubTowService;

import lombok.extern.log4j.Log4j2;

@Transactional
@Service
@Log4j2
public class SubTowServiceImpl implements SubTowService {

	@Autowired
	private SubTowMapper subTowMapper;
	@Autowired
	private ExaminMapper examinMapper;
	
	/**
	 * 查询科目二 （所有学员）信息成功
	 */
	@Override
	public BaseResponse<List<SubjectTowPO>> selectall() {
		List<SubjectTowPO> list = subTowMapper.selectList(null);
		if (!"[]".equals(list.toString())) {
			log.info("查询科目二信息成功"+list.toString());
			return BaseResponse.successOf(list);
		}
		return BaseResponse.message(ResponseEnum.ZWSJ);
	}
	
	
	/**
	 * 科目二录入数据
	 */
	@Override
	public BaseResponse<?> insert(SubInsertReqDto subInsertReqDto) {
		EntityWrapper<SubjectTowPO> wrapper = new EntityWrapper<>();
		wrapper.eq("identity", subInsertReqDto.getIdentity());
		List<SubjectTowPO> list = subTowMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())) {
			log.info("已经录入");
			return BaseResponse.message(ResponseEnum.YJLR);
		}
		Integer isSuccess = 0;
		try {
			SubjectTowPO SubjectTowPO = new SubjectTowPO();
			BeanUtils.copyProperties(SubjectTowPO, subInsertReqDto);
			log.info("cooy后："+SubjectTowPO.toString());
			Integer row = subTowMapper.insert(SubjectTowPO);
			if (row == 1) {
				log.info("科目二信息录入成功");
				isSuccess+=1;
				/*return BaseResponse.message(ResponseEnum.LRCG);*/
			}	
		} catch (Exception e) {
			log.info(e);
		}
		/**
		 * 更新考试信息
		 */
		EntityWrapper<ExaminPO> wrapperExamin = new EntityWrapper<>();
		wrapperExamin.eq("identity",subInsertReqDto.getIdentity());
		List<ExaminPO> listExamin = examinMapper.selectList(wrapperExamin);
		if (!"[]".equals(listExamin.toString())) {		
			ExaminPO examinPO = new ExaminPO();
			examinPO.setSub02(subInsertReqDto.getScore());
			examinPO.setSubMeg02(subInsertReqDto.getIsrepeat());
			Integer row = examinMapper.update(examinPO, wrapperExamin);
			if (row == 1) {
				log.info("考试信息更新成功");
				isSuccess+=1;
			}
		}else {
			ExaminPO examinPO = new ExaminPO();
			examinPO.setSub02(subInsertReqDto.getScore());
			examinPO.setSubMeg02(subInsertReqDto.getIsrepeat());
			examinPO.setIdentity(subInsertReqDto.getIdentity());
			examinPO.setName(subInsertReqDto.getName());
			Integer row = examinMapper.insert(examinPO);
			if (row == 1) {
				log.info("插入考试信息成功");
				isSuccess+=1;
			}
		}	
		if (isSuccess == 2) {
			return BaseResponse.message(ResponseEnum.LRCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	@Override
	public BaseResponse<?> delete(IdReqDto idReqDto) {
		SubjectTowPO subOne = subTowMapper.selectById(idReqDto.getId());
		Integer row = subTowMapper.deleteById(idReqDto.getId());
		if (row == 1) {
			ExaminPO examinPO = new ExaminPO();
			examinPO.setSub02(null);
			examinPO.setSubMeg02("");
			EntityWrapper<ExaminPO> wrapper = new EntityWrapper<>();
			wrapper.eq("identity", subOne.getIdentity());
			examinMapper.update(examinPO, wrapper);
			return BaseResponse.message(ResponseEnum.SQCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	@Override
	public BaseResponse<?> updateById(SubOneUpdateByIdReqDto subOneUpdateByIdReqDto) {
		
		Integer isSuccess = 0;
		try {
			SubjectTowPO SubjectTowPO = new SubjectTowPO();
			BeanUtils.copyProperties(SubjectTowPO, subOneUpdateByIdReqDto);
			log.info("copy后："+SubjectTowPO.toString());
			Integer row = subTowMapper.updateById(SubjectTowPO);
			if (row == 1) {
				isSuccess += 1;
				log.info("更新科目二信息成功");
				//return BaseResponse.message(ResponseEnum.GXCG);
			}
		} catch (Exception e) {
			log.info(e);
		}
		/**
		 * 更新考试信息
		 */
		EntityWrapper<ExaminPO> wrapperExamin = new EntityWrapper<>();
		wrapperExamin.eq("identity",subOneUpdateByIdReqDto.getIdentity());
		List<ExaminPO> listExamin = examinMapper.selectList(wrapperExamin);
		if (!"[]".equals(listExamin.toString())) {		
			ExaminPO examinPO = new ExaminPO();
			examinPO.setSub02(subOneUpdateByIdReqDto.getScore());
			examinPO.setSubMeg02(subOneUpdateByIdReqDto.getIsrepeat());
			Integer row = examinMapper.update(examinPO, wrapperExamin);
			if (row == 1) {
				log.info("考试信息更新成功");
				isSuccess += 1;
			}
		}else {
			ExaminPO examinPO = new ExaminPO();
			examinPO.setSub02(subOneUpdateByIdReqDto.getScore());
			examinPO.setSubMeg02(subOneUpdateByIdReqDto.getIsrepeat());
			examinPO.setIdentity(subOneUpdateByIdReqDto.getIdentity());
			examinPO.setName(subOneUpdateByIdReqDto.getName());
			Integer row = examinMapper.insert(examinPO);
			if (row == 1) {
				log.info("插入考试信息成功");
				isSuccess += 1;
			}
		}	
		if (isSuccess == 2) {
			return BaseResponse.message(ResponseEnum.GXCG);
		}	
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	@Override
	public BaseResponse<SubjectTowPO> selectById(IdReqDto idReqDto) {
		SubjectTowPO SubjectTowPO = subTowMapper.selectById(idReqDto.getId());
		if (SubjectTowPO != null) {
			return BaseResponse.successOf(SubjectTowPO);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}
	
	@Override
	public BaseResponse<List<SubjectTowPO>> selectInput(SubInputReqDto subInputReqDto) {
		EntityWrapper<SubjectTowPO> wrapper = new EntityWrapper<>();
		if (subInputReqDto.getIdentity() != null && subInputReqDto.getIdentity() != "") {
			wrapper.eq("identity", subInputReqDto.getIdentity());
		}
		if (subInputReqDto.getName() != null && subInputReqDto.getName() != "") {
			wrapper.like("name", subInputReqDto.getName());
		}
		List<SubjectTowPO> list = subTowMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())){
			log.info("筛选框查询结果："+list.toString());
			return BaseResponse.successOf(list);
		}
		return BaseResponse.successOf(null);
	}

}
